// pages/signin/signin.js
Page({
  data:{
      account:'',
      pwd:'',
      hidden:true,
      btnText:'获取验证码',
      validing:false,
      timer:''
  },
  confirm(){
    this.setData({hidden:true})
  },
  accountinput(e){
    this.setData({account:e.detail.value})
  },
  pwdinput(e){    
      this.setData({pwd:e.detail.value})
  },
  getValid(){
    let self = this;
    if(self.data.account.length==0){
      self.setData({
        title:"请输入手机号",
        hidden:false
      })
      return;
    }
    if (self.mydata.requesting) return;
    self.countDown();
    self.mydata.requesting = true;
      getApp().request({
        url: 'send_msg_code', 
        data: {
          mobile: self.data.account 
        },
        success: function(res) {
          console.log(res);
            if(!res.data.flag)
            self.setData({
                title:res.data.msg,
                hidden:false,
                btnText: '重新获取',
                validing: false
            })
        },fail(res){
          clearInterval(self.mydata.timer);
          self.setData({
            title:"获取验证码失败",
            hidden:false,
            btnText: '重新获取',
            validing: false
          })
          console.log(res);
        },complete(){
        }
      },true)
  },
  mydata:{
    requesting:false
  },
  countDown(){
    var self = this;
    var time = 60;
    var timer = setInterval(()=>{
        if(time<=0){
            clearInterval(timer);
            self.setData({
                btnText:'重新获取',
                validing:false
            })
            self.mydata.requesting = false;
            return;
        }else{
            self.setData({
                btnText:time,
                validing:true
            })
            time--;
        }
    },1000)
    self.mydata.timer=timer;
  },nextStep(){
      var self = this;
      if(self.data.pwd.length==0){
        self.setData({
            title:"请输入验证码",
            hidden:false
        })
        return;
        }
      getApp().request({
      url: 'varify_msg_code', 
      data: {
        code: self.data.pwd 
      },
      success(res) {
        if(res.data.flag){
            wx.redirectTo({url: '../setpwd/setpwd?mobile='+self.data.account})
        }else{
            self.setData({
              title:res.data.msg,
              hidden:false
          })
        }
      },fail(){
        self.setData({
            title:"请求失败",
            hidden:false
        })
      },complete(){
        
      }
    })
  }
})